package com.mfhcd.facepay.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatEditText;

import com.mfhcd.facepay.App;
import com.mfhcd.facepay.R;
import com.mfhcd.facepay.utils.AppUtils;
import com.mfhcd.facepay.utils.EditTextUtils;

import static androidx.appcompat.content.res.AppCompatResources.getDrawable;


/**
 * @author 徐长策
 * E-Mail: xucc@mfhcd.com
 * Date：2018/11/14
 * 版权所有 © 现代金融（成都）控股有限公司
 */
public class CancelEditText extends AppCompatEditText implements TextWatcher, View.OnFocusChangeListener {
    private Drawable drawable_clear;

    private ClearCallBack clearCallBack;

    private boolean hasFocus;

    public interface ClearCallBack {
        void onClear();
    }

    public void setClearCallBack(ClearCallBack clearCallBack) {
        this.clearCallBack = clearCallBack;
    }

    public CancelEditText(Context context) {
        super(context);
        init();
    }

    public CancelEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CancelEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        drawable_clear = getCompoundDrawables()[2];
        drawable_clear = getDrawable(getContext(), R.drawable.common_edit_clear);
        drawable_clear.setBounds(0, 0, AppUtils.dipToPixel(App.getInstance(), 16), AppUtils.dipToPixel(App.getInstance(), 16));
        addTextChangedListener(this);
        setOnFocusChangeListener(this);
        setClearIconVisible(false);
        setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        EditTextUtils.setEditTextInhibitInputSpeChat(this, false);
        setClearIconVisible(hasFocus && text.length() > 0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));

                if (touchable) {
                    this.setText("");
                    if (clearCallBack != null) {
                        clearCallBack.onClear();
                    }
                }
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFocus = hasFocus;
        if (hasFocus && getText().length() > 0) {
            setClearIconVisible(true); // 有焦点且有文字时显示图标
        } else {
            setClearIconVisible(false);
        }
    }

    /**
     * 设置清除图标的显示与隐藏，调用setCompoundDrawables为EditText绘制上去
     */
    protected void setClearIconVisible(boolean visible) {
        setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], visible ? drawable_clear : null,
                getCompoundDrawables()[3]);
    }
}
